package net.xdclass.onlinexdclass.service.impl;

import net.xdclass.onlinexdclass.mapper.UserMapper;
import net.xdclass.onlinexdclass.model.entity.User;
import net.xdclass.onlinexdclass.service.UserService;
import net.xdclass.onlinexdclass.templates.JdbcTemplateUtil;
import net.xdclass.onlinexdclass.utils.CommonUtils;
import net.xdclass.onlinexdclass.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JdbcTemplateUtil jdbcTemplateUtil;


    public static Map<String, User> sessionMap = new HashMap<>();


    @Override
    public int save(Map<String, String> userInfo) {

        User user = parseToUser(userInfo);

        if (null != user) {
            return userMapper.save(user);
        } else {
            return -1;
        }

    }

    //@CachePut方法被调用，然后结果被缓存
    @Override
//    @CachePut(value = "uservalue", key = "#user.getId()")
    @CacheEvict(value = "uservalue", key = "#user.getId()")
    public void updateUser(User user) {
        if (null == user) {
            return;
        }
        userMapper.updateUser(user);
    }


    //登录
    @Override
    public String findByPhoneAndPwd(String phone, String pwd) {

        User user = userMapper.findByPhoneAndPwd(phone, CommonUtils.MD5(pwd));
        if (null != user) {
            return JWTUtils.geneJsonWebToken(user);
        } else {
            return null;
        }


    }

    @Override
    @Cacheable(value = "uservalue", key = "#id")
    public User getUserById(Integer id) {

        User userById = userMapper.getUserById(id);
        if (null != userById) {
            return userById;
        } else {
            return null;
        }


    }

    @Override
    public User findUserByToken(Integer id) {
        Object[] objects = {id};
        User user = jdbcTemplateUtil.selectPojo("select * from user where id = ?", User.class, objects);
        if (null == user) {
            return null;
        }
        return user;
    }

    /**
     * 解析 user对象
     *
     * @param userInfo
     * @return
     */
    private User parseToUser(Map<String, String> userInfo) {
        if (userInfo.containsKey("phone") && userInfo.containsKey("pwd") && userInfo.containsKey("name")) {
            User user = new User();
            user.setName(userInfo.get("name"));
            user.setHeadImg(getRandomImg());
            user.setCreateTime(new Date());
            user.setPhone(userInfo.get("phone"));

            String pwd = userInfo.get("pwd");


            //MD5加密,密码不能明文保存进去，一定要进行加密
            user.setPwd(CommonUtils.MD5(pwd));
            return user;
        } else {
            return null;
        }

    }

    /**
     * 放在CDN上的随机头像
     */
    private static final String[] headImg = {
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    private String getRandomImg() {
        int size = headImg.length;
        Random random = new Random();
        int index = random.nextInt(size);
        return headImg[index];
    }


}
